﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;
using System.Reflection;

namespace  Maticsoft.Common
{
    public class FileHelp
    {

        #region "根据路径读取文件内容  "
        /// 根据路径读取文件内容
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string ReadFile(string path)
        {
            string str = "";

            try
            {
                StreamReader sr = new StreamReader(path, Encoding.GetEncoding("gb2312"));
                str = sr.ReadToEnd();
                sr.Close();

                return str;
            }
            catch
            {
                return str;
            }
        }
        #endregion

        #region "向指定的文件写入指定的内容；"
        /// <summary>
        /// 向指定的文件写入指定的内容；
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="fileContent"></param>
        public static void WriteToFile(string path, string fileName, string fileContent,bool append)
        {
            Directory.CreateDirectory(path);
            using (StreamWriter sw = new StreamWriter(path + fileName, append, Encoding.GetEncoding("utf-8")))
            {
                sw.Write(fileContent);
            }
        }

        public static void WriteToFile(string fileName, string fileContent,bool append)
        {
            if (!File.Exists(fileName))
            {
                File.Create(fileName).Close();
            }
            
            using (StreamWriter sw = new StreamWriter(fileName, append, Encoding.GetEncoding("utf-8")))
            {
                sw.Write(fileContent);
            }
        }
        #endregion
    }
}
